import 'dart:io';

import 'package:android_intent_plus/android_intent.dart';
import 'package:flutter/material.dart';
import 'package:get/get_utils/src/extensions/internacionalization.dart';
import 'package:ljy_p_app/diffwidget/ljy_tabbar_widget.dart';
import 'package:ljy_p_app/diffwidget/ljy_title_bar.dart';
import 'package:ljy_p_app/home/widget/home_drawer.dart';
import 'package:ljy_p_app/localization/localization_key.dart';
import 'package:ljy_p_app/pages/dynamic/dynamic_page.dart';
import 'package:ljy_p_app/pages/mine/my_page.dart';
import 'package:ljy_p_app/pages/trend/trend_page.dart';
import 'package:ljy_p_app/style/lj_style.dart';
import 'package:lottie/lottie.dart';

class LjHomePage extends StatelessWidget {
  LjHomePage({super.key});

  final GlobalKey rightKey = GlobalKey();

  /// 不退出
  _dialogExitApp(BuildContext context) async {
    ///如果是 android 回到桌面
    if (Platform.isAndroid) {
      AndroidIntent intent = AndroidIntent(
        action: 'android.intent.action.MAIN',
        category: "android.intent.category.HOME",
      );
      await intent.launch();
    }
  }

  /// tab样式
  _renderTab(icon, text) {
    return Tab(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(
            icon,
            size: 16.0,
          ),
          Text(text)
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> tabs = [
      _renderTab(LJICons.MAIN_DT, LocalizationKey.homeDynamic.tr),
      _renderTab(LJICons.MAIN_QS, LocalizationKey.homeTrend.tr),
      _renderTab(LJICons.MAIN_MY, LocalizationKey.homeMy.tr)
    ];

    /// 增加返回按键监听
    return PopScope(
        canPop: false,
        onPopInvokedWithResult: (didPop, _) {
          _dialogExitApp(context);
        },
        child: LjyTabbarWidget(
          drawer: HomeDrawer(),
          type: TabType.bottom,
          tabItems: tabs,
          tabViews: [DynamicPage(), TrendPage(), MyPage()],
          onDoublePress: (index) {},
          backgroundColor: LJColors.primarySwatch,
          indicatorColor: LJColors.white,
          title: LjyTitleBar(
            LocalizationKey.appName.tr,
            rightWidget: InkWell(
              onTap: () {},
              child: Container(
                key: rightKey,
                alignment: Alignment.centerRight,
                child: Lottie.asset('assets/file/search.json',
                    width: 70,
                    height: 80,
                    fit: BoxFit.cover,
                    alignment: Alignment.centerRight),
              ),
            ),
          ),
        ));
  }
}
